package com.lala.config;

import com.lala.util.ResultJson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author 霍明铭
 * @date 2020/3/29 21:53
 */
@Controller
@RequestMapping("/upload")
public class UploadPicture {

    @PostMapping("/picture")
    @ResponseBody
    public ResultJson picture(@RequestParam("file")MultipartFile file, HttpServletRequest request) throws IOException {
        String path=request.getServletContext().getRealPath("/");
        File uping=new File(path+"upload");
        if (!uping.exists()){
            uping.mkdirs();
        }
        String name=UUID.randomUUID().toString();
         String newpath=uping.getAbsolutePath()+"\\"+ name+".png";
        File f=new File(newpath);
        file.transferTo(f);
        return   new ResultJson(200,"成功","/upload/"+name+".png");
    }

}
